<template>
	<view>
		<!-- 用户资料 -->
		<uni-popup ref="showVerify" type="center" border-radius="10px" background-color="#70552ddd">
			<view class="" style="width: 90vw;padding: 20px 0;">

				<view
					style="display: flex;flex-direction: column;align-items: center;font-weight: bold;color:  #f3eacd;gap: 10px;padding: 30px 15px;font-size: large;">
					<view class="">欢迎回到悦玺云商</view>
					<view v-show="needBindPhone">系统将采集您的手机号码仅用于提供身份标识、订单查询等服务。</view>
				</view>

				<view
					style="color: #f3eacd;font-size: small;display: flex;justify-content: center;align-items: center;">
					<tn-checkbox v-model="agent" active-color="#70552d" checked-shape="circle"
						style="padding: 15px;"></tn-checkbox>
					<text>我同意</text>
					<text @tap="()=>{this.showSecret = true}" style="color: aqua;">《用户服务协议》</text>
					<text @tap="()=>{this.showSecret = true}" style="color: aqua;">《隐私协议》</text>
				</view>

				<view style="padding: 10px;">
					<tn-button v-if="needBindPhone" shape="round" text-color="#70552d" bg-color="#f1f1f1"
						open-type="getPhoneNumber" debounce :disabled="!agent" @getphonenumber="onRegeditUserinfo"
						:custom-style="{width:'100%',padding:'15px 0',fontSize:'large'}">授权手机号登录</tn-button>

					<tn-button v-else shape="round" text-color="#70552d" bg-color="#f1f1f1" debounce
						@click="onfastLogin"
						:custom-style="{width:'100%',padding:'15px 0',fontSize:'large'}">快速登录</tn-button>

				</view>
				<view style="padding: 10px;display: flex;justify-content: center;color: #f3eacd;">
					<view @click="close">放弃登录</view>
				</view>

			</view>
		</uni-popup>

		<!-- popup弹窗 -->
		<tn-popup v-model="showSecret" open-direction="bottom" :overlay-closeable="false">
			<tn-switch-tab v-model="curConcert" :tabs="['用户协议', '隐私声明']">
				<subSecretCardVue v-if="curConcert === 0" src="/api/doc/" @eventClose="()=>{this.showSecret = false}"
					@eventAgree="eventAgree">
				</subSecretCardVue>
				<subSecretCardVue v-if="curConcert === 1" src="/api/dod/" @eventClose="()=>{this.showSecret = false}"
					@eventAgree="eventAgree">
				</subSecretCardVue>
			</tn-switch-tab>
			<view class="" style="padding: 20px;">
				<button open-type="agreePrivacyAuthorization" @agreeprivacyauthorization="eventAgree"
					style="background-color: #000;color: #fff;">接受协议</button>
			</view>
			<view class="" style="height: 60px;">

			</view>
		</tn-popup>
	</view>
</template>

<script>
	import psMethod from '@/common/psMethod';
	import UniToCos from '@/js_sdk/com-ls-cos/components/ls-cos'
	import subSecretCardVue from '@/components/sub-secret-card/sub-secret-card.vue';
	export default {
		name: "person-verify-pop",
		data() {
			return {
				title: '用户资料',
				sConf: getApp().globalData,
				// 改资料
				agent: false,
				isNotReg: true,
				showSecret: false,
				avatarUrl: 'https://cos.yuejunxi.com/media/default/default_0.png',
				avatarNam: '新用户',
				curConcert: 0,
				needBindPhone: true
			};
		},
		components: {
			subSecretCardVue
		},
		methods: {
			open() {
				console.log('open', );
				this.needBindPhone = this.sConf.auth.phone ? false : true
				this.$refs.showVerify.open()
			},
			close() {
				this.$refs.showVerify.close()
			},
			/**
			 * 加载用户头像昵称
			 */
			onChooseAvatar(e) {
				console.log('onChooseAvatar', e);
				this.avatarUrl = e.detail.avatarUrl
				if (this.avatarUrl.includes('default/default') || !this.avatarNam || this.avatarNam.length === '新用户') {
					this.isNotReg = true
				} else {
					this.isNotReg = false
				}
			},
			/**
			 * 验证用户昵称
			 */
			onChooseName(e) {
				console.log('onChooseName', e);
				this.avatarNam = e.detail.value
				if (this.avatarUrl.includes('default/default') || !this.avatarNam || this.avatarNam.length === '新用户') {
					this.isNotReg = true
				} else {
					this.isNotReg = false
				}
			},
			/**
			 * 写入本地数据
			 */
			saveLocalCacheData(token, auth) {
				let prefix = this.sConf.run === 'trial' ? 'dev' : ''
				try {
					uni.setStorageSync(prefix + 'token', token);
					uni.setStorageSync(prefix + 'auth', auth);
					uni.setStorageSync(prefix + 'last', Date.now());
				} catch (e) {
					console.log('写入本地缓存失败');
				}
			},
			/**
			 * 快速登录
			 */
			onfastLogin() {
				console.log('onfastLogin');
				if (!this.agent) {
					uni.showModal({
						title: '错误',
						content: '请仔细阅读并同意平台用户服务协议',
						showCancel: false
					})
					return
				}
				this.saveLocalCacheData(this.sConf.token, this.sConf.auth)
				this.$emit('reload')
				this.close()
			},
			/**
			 * 注册手机用户
			 */
			async onRegeditUserinfo(e) {
				console.log('onRegeditUserinfo', e);

				uni.showLoading({
					title: '正在注册'
				})
				console.log('eventPhoneLogin', e.detail.code);
				let host = this.sConf.host + '/weapps/phone/'
				let data = {
					code: e.detail.code,
					sid: this.sConf.auth.id
				}

				psMethod.requestPostSync(host, data).then(res => {
					console.log('手机登录成功', res);
					this.sConf.token = res.resmsg.token
					this.sConf.auth = res.resmsg.auth
					this.saveLocalCacheData(res.resmsg.token, res.resmsg.auth)
					this.$emit('reload')
					this.close()
				}).catch(err => {
					console.log('手机登录失败', err);
					uni.showModal({
						title: '登录失败',
						content: err.resmsg,
						showCancel: false
					})
				}).finally(() => {
					uni.hideLoading()
				})
			},
			/**
			 * 上传图片
			 */
			getUploadCos() {
				console.log('加载cos参数');
				return new Promise((resolve, reject) => {
					uni.request({
						url: this.sConf.host + '/api/cosSt/',
						method: 'POST',
						header: {
							'content-type': 'application/json',
							'authorization': this.sConf.token
						},
						data: {},
						success(res) {
							resolve(res.data.resmsg)
						},
						fail(err) {
							reject(err)
						}
					})
				})
			},
			cosUploadFile(cosDict, file) {
				// 传入bucket和region
				let cos = new UniToCos(cosDict.account.bucket, cosDict.account.region)
				// 传入secretId,secretKey,sessionToken
				cos.createCos(cosDict.credentials.tmpSecretId, cosDict.credentials.tmpSecretKey, cosDict.credentials
					.sessionToken)
				// 传入filePath和fileName
				let filePath = file;
				let fileName = 'media/face/' + this.sConf.auth.id + '.jpg';

				return new Promise((resolve, reject) => {
					cos.wxUploadFile(filePath, fileName).then(res => {
						console.log("上传图片", res.Location.substr(res.Location.indexOf('/')))
						resolve(res.Location.substr(res.Location.indexOf('/')))
					}).catch(err => {
						reject(err)
					})
				})
			},
			/**
			 * 同意用户协议
			 */
			eventAgree(e) {
				console.log('eventAgree', e);
				this.agent = true
				this.showSecret = false
			}
		}
	}
</script>

<style lang="scss" scoped>

</style>